<!--图片上传控件，改造自mpvue-weui-->
<template>
  <div class="weui-uploader">
    <div class="weui-uploader__bd">
      <div v-if="showType" class="weui-uploader__files" id="uploaderFiles">
        <div v-for="(item ,index) in files" :key="index">
          <div class="weui-uploader__file">
            <div class="weui-uploader__img pos-rel">
              <img v-if="fileSpace" :src="fileSpace + '/images/icon-play-8.png'" alt=""
                class="weui-uploader__img absolute-img" @click="fileVideo = true">
              <video :src="item" class="weui-uploader__img" :controls="false"></video>
            </div>
            <div v-if="fileVideo" class="video-layer" catchtouchmove="true">
              <img v-if="fileSpace" :src="fileSpace + '/images/layer-close.png'" alt=""
                @click="fileVideo = false" class="width-26 height-26">
              <video :src="item" autoplay="autoplay" controls="controls"></video>
            </div>            
            <div class="delete-icon" @click="deleteImg" :id="item" :data-index="index" :key="index"></div>
          </div>
        </div>
      </div>
      <div v-if="files.length < maxLength" class="weui-uploader__input-box">
        <div class="weui-uploader__input" @click="chooseVideo"></div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isComponents: true,
        files: [],
        fileVideo: false
      }
    },
    onLoad() {
      // 初始化data
      Object.assign(this.$data, this.$options.data())
    },
    props: {
      uploadFiles: {
        type: Array,
        default: []
      },
      maxLength: {
        type: Number,
        default: 20
      },
      showType: {
        type: Boolean,
        default: true
      }
    },
    mounted() {
      this.files = this.files.concat(this.uploadFiles)
    },
    methods: {
      chooseVideo(e) {
        this.showType = true
        let that = this
        if (!(this.files.length > this.maxLength - 1)) {
          that.$pt.chooseVideo({
            success: function(res) {
              that.files = that.files.concat(res.tempFilePath)
              res.files = that.files
              that.$emit('upLoadSuccess', res)
            },
            fail: function(res) {
              that.$emit('upLoadFail', res)
              // 修改提交记录
            },
            complete: function(res) {
              that.$emit('upLoadComplete', res)
            }
          })
        } else {
          that.$utils.showToast({
            title: `最多上传 ${this.maxLength} 张图片`,
            icon: 'none',
            mask: true
          })
        }
      },
      deleteImg(e) {
        Array.prototype.indexOf = function(val) { // eslint-disable-line
          for (let i = 0; i < this.length; i++) {
            if (this[i] === val) return i
          }
          return -1
        }
        Array.prototype.remove = function(val) { // eslint-disable-line
          let index = this.indexOf(val)
          if (index > -1) {
            this.splice(index, 1)
          }
        }
        this.files.remove(e.currentTarget.id)
        let uploadDeleteObj = {
          src: e.currentTarget.id,
          index: e.currentTarget.dataset.index,
          files: this.files
        }
        this.$emit('uploadDelete', uploadDeleteObj)
      }
    }
  }
</script>

<style lang="less" scoped>
  .absolute-img{
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
  }
  .video-layer{
    &:after{
      content: '';
      display: block;
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      z-index: 100;
      background: rgba(0, 0, 0, 0.5);
    }
    img{
      position: fixed;
      right: 15px;
      top: 15px;
      z-index: 102;
    }
    video{
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      z-index: 101;
    }    
  }
  .weui-uploader__file {
    position: relative;
  }
  .weui-uploader__bd {
    overflow: visible;
  }
  .delete-icon {
    position: absolute;
    width: 20px;
    height: 20px;
    background: #f43530;
    right: -10px;
    top: -10px;
    border-radius: 20px;
    z-index: 5;
  }
  .delete-icon::before {
    content: '';
    width: 13px;
    height: 2px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
  }
</style>
